package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;
import jdk.nashorn.internal.ir.Flags;

public class TeacherService {
    private TeacherDao dao = new TeacherDao();

    public boolean addTeacher(Teacher tea) {
        //1.创建TeacherDao(库管)对象

        //2.将学生对象,传递给TeacherDao库管中的 addTeacher方法
        //3.将返回的boolean类型结果,返还给TeacherController
        return dao.addTeacher(tea);
    }

    //业务员中的判断是否存在功能
    public boolean isExists(String id) {
        //1.拿到系统中已有的所有学号信息
        //1.1指挥库管将系统中所有老师信息查找出来

        Teacher[] allTeachers = dao.findAllTeachers();
        //2.拿到校验的id去已有的学号中遍历查找
        //2.1先假设要查找的id不存在
        boolean flag = false;

        //2.2遍历所有学生信息
        for (int i = 0; i < allTeachers.length; i++) {
            //2.3拿到每一个学生对象的学号
            Teacher tea = allTeachers[i];
            //2.4 当前索引位置的学生不为null,并且该学生的学号和要查找的学号
            if (tea != null && tea.getId().equals(id)) {
                flag = true;
            }
        }

        return flag;
    }


    public Teacher[] findAllTeachers() {

        Teacher[] allTeachers = dao.findAllTeachers();

        boolean flag = false;
        //遍历allTeachers
        for (int i = 0; i < allTeachers.length; i++) {
            //拿到每一个索引位置的元素进行判断
            if (allTeachers[i] != null) {

                flag = true;
                break;
            }
        }
        //根据flag的值来决定本次方法的返回值
        if (flag) {

            return allTeachers;
        } else {
            return null;
        }
    }

    public void deleteTeacherById(String delId) {
        dao.deleteTeacherById(delId);
    }

    public void updateTeacherById(String updateId, Teacher newtea) {
        dao.updateTeacherById(updateId,newtea);
    }
}


